#include "commonpage.h"
#include "ui_commonpage.h"

CommonPage::CommonPage(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::CommonPage)
{
    ui->setupUi(this);
    //移除水平滚动条
    ui->pageMusicList->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    //当点击播放全部的时候，就需要发送playAll信号，然后给Music类来做对应的处理
    connect(ui->playAllBtn,&QPushButton::clicked,this,[=](){
        emit playAll(commonPageType);
    });
    //处理双击音乐的时候播放音乐的操作，这里需要music类来处理
    connect(ui->pageMusicList,&QListWidget::doubleClicked,this,[=](const QModelIndex& index){
        emit playMusicByIndex(this,index.row());
    });
}

CommonPage::~CommonPage()
{
    delete ui;
}

void CommonPage::setCommonPageUI(const QString &title, const QString &image)
{
    //设置标题以及图片
    ui->pageTitle->setText(title);
    ui->musicImageLabel->setPixmap(QPixmap(image));
    //将图片覆盖满当前的label
    ui->musicImageLabel->setScaledContents(true);

    //测试
//    ListItemBox* itembox = new ListItemBox(this);
//    QListWidgetItem* listWidgetItem = new QListWidgetItem(ui->pageMusicList);
//    listWidgetItem->setSizeHint(QSize(ui->pageMusicList->width(),45));
//    ui->pageMusicList->setItemWidget(listWidgetItem,itembox);
}

void CommonPage::setMusicListType(PageType type)
{
    commonPageType = type;
}

void CommonPage::addMusicToPage(MusicList &musicList)
{
    //把list中的音乐按照类型区分，并放入每一个page中，使用id即可
    //需要清空上一次使用的list
    musicListOfPage.clear();
    for(auto& musicInfo : musicList)
    {
        //根据不同的页面类型，放入列表中
        switch(commonPageType)
        {
        case LOCAL_PAGE:
            musicListOfPage.push_back(musicInfo.getMusicId());
            break;
        case LIKE_PAGE:
            if(musicInfo.getIsLike())
            {
                musicListOfPage.push_back(musicInfo.getMusicId());
            }
            break;
        case HISTORY_PAGE:
            if(musicInfo.getIsHistory())
            {
                musicListOfPage.push_back(musicInfo.getMusicId());
            }
            break;
        default:
            break;
        }
    }
}

void CommonPage::reFresh(MusicList &musicList)
{
    //将QListWidget之前已经添加的内容清空
    ui->pageMusicList->clear();
    //1.需要把list中的音乐进行分类处理
    addMusicToPage(musicList);
    //2.添加到当前页面中
    for(auto& musicId : musicListOfPage)
    {
        //2.1在musicList中找到对应的Info信息
        auto it = musicList.findMusicById(musicId);
        if(it == musicList.end()) continue;
        //2.2创建一个itembox填充
        ListItemBox* itemBox = new ListItemBox(ui->pageMusicList);
        itemBox->setMusicName(it->getMusicName());
        itemBox->setSinger(it->getSingerName());
        itemBox->setAlbumName(it->getAlbumName());
        itemBox->setLikeIcon(it->getIsLike());

        QListWidgetItem* listWidgetItem = new QListWidgetItem(ui->pageMusicList);
        listWidgetItem->setSizeHint(QSize(ui->pageMusicList->width(),45));
        ui->pageMusicList->setItemWidget(listWidgetItem,itemBox);

        //这里需要把其中任意一个控件发送的收藏信号传递给上层进行处理 -- 在我喜欢界面添加对应的控件
        connect(itemBox,&ListItemBox::setIslike,this,[&](bool isLike){
           emit updateLikeMusic(isLike,musicId);
        });
    }
    //3.进行刷新界面显示出来
    ui->pageMusicList->repaint();
}

void CommonPage::addMusicToPlayer(MusicList &musicList, QMediaPlaylist *playList)
{
    //根据不同的界面的类型来添加到对应的playList中
    for(auto& musicInfo : musicList)
    {
        switch(commonPageType)
        {
        case LIKE_PAGE:
        {
            if(musicInfo.getIsLike())
            {
                playList->addMedia(musicInfo.getMusicUrl());
            }
            break;
        }
        case HISTORY_PAGE:
        {
            if(musicInfo.getIsHistory())
            {
                playList->addMedia(musicInfo.getMusicUrl());
            }
            break;
        }
        case LOCAL_PAGE:
        {
            playList->addMedia(musicInfo.getMusicUrl());
        }
        default:
            break;
        }
    }
}

const QString &CommonPage::getMusicIdByIndex(int index) const
{
    //通过id拿到对应的音乐id
    if(index >= musicListOfPage.size())
    {
        qDebug() << "无此音乐";
        return "";
    }
    return musicListOfPage[index];
}

void CommonPage::setImageLabel(QPixmap pixMap)
{
    ui->musicImageLabel->setPixmap(pixMap);
    ui->musicImageLabel->setScaledContents(true);
}


